<template>
  <view>
    <view class="input-search" v-if="scan_keyword==''?true:false">
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" :placeholderStyle="placeholderStyle"></uni-easyinput>
    	<button @click="search">搜索</button>
    </view>
	
	<view class="input-search" v-if="scan_keyword==''?false:true">
		<uni-easyinput prefixIcon="scan" disabled :styles="styles" placeholder="请扫存货、库区二维码" :placeholderStyle="placeholderStyle"></uni-easyinput>
		<button @click="reset">重置</button>
	</view>
	
    <uni-table ref="table" type="" stripe :loading="loading" border emptyText="暂无更多数据" filter-type="search">
		<uni-tr v-for="(item, index) in card_arr" :key="index" >
			<uni-td width="100%" align="left" class="td-class" >
				<div @click="selectes(item)" class="td_div">
					<view class="td_list">
						<text class="label">名称：</text>
						<text class="text font-red">{{ item.name }}</text>
					</view>
					<view class="td_list">
						<text class="label">编码：</text>
						<text class="text">{{ item.cInvCode }}</text>
					</view>
					<view class="td_list">
						<text class="label">规格：</text>
						<text class="text" >{{ item.specification }}</text>
						
					</view>
					<view class="td_list">
						<text class="label">基本数量：</text>
						<text class="text" style="min-width:200rpx">{{ item.iQuantity }}</text>
						<text class="label" style="min-width: 140rpx;">单位：</text>
						<text class="text">{{ item.unit }}</text>
					</view>
					<view class="td_list">
						<text class="label">常用数量：</text>
						<text class="text" style="min-width:200rpx">{{ item.store_quantity }}</text>
						<text class="label" style="min-width: 140rpx;">单位：</text>
						<text class="text">{{ item.store_unit }}</text>
					</view>
					<view class="td_list">
						<text class="label">批号：</text>
						<text class="text">{{ item.cBatch }}</text>
					</view>
					<view class="td_list">
						<text class="label">位置：</text>
						<text class="text">{{ item.shelves_name }}</text>
					</view>
				</div>
			</uni-td>
        </uni-tr>
    </uni-table>
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
	<scanCode :scanType="0"></scanCode>
  </view>
</template>

<script>
// 引入uni-ui组件
import {
  uniPagination,
  uniIcons,
  uniCard,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput
} from "@dcloudio/uni-ui"
import scanCode from "@/component/scan-code.vue"
//import baTreePicker from "@/components/ba-tree-picker/ba-tree-picker.vue"
export default {
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
      item: '',
      id: '',
	  
	  placeholderStyle:'font-size:17px;color:red',
	  styles:{
	  	borderColor: '#999'
	  },
	  keyword:'',
	  scan_keyword:'',
	  where:' 1=1 ',
	  inv_id:0,
	  stock_id:uni.getStorageSync('user').stock_id,
	  index:0,
	  
	  //在线版
	  config:uni.getStorageSync('myconfig'),
	  pageinfo:{},
	  //
    }
  },
  components: {
    uniPagination,
    uniIcons, //uni-ui的icons组件
    uniCard, //uni-ui的卡片组件
    uniTable,
    uniTr,
    uniTh,
    uniTd,
	uniEasyinput,scanCode
  },
  onPullDownRefresh() {
  	this.search()
  	uni.stopPullDownRefresh();
  },
  methods: {
    // 点击关闭的事件回调
    close() {
      uni.navigateBack({})
    },
	// 搜索
	search() {
		this.currentPage = 1
		this.getlist();
	},
    //获取列表
    getlist() {
		uni.showLoading();
		if(!this.config.online){
			this.where = ' iQuantity>0 '
			
			if(this.keyword!=''){
				this.where += " and (name like '%"+this.keyword+"%' or specification like '%"+this.keyword+"%' or cInvCode like '%"+this.keyword+"%' or shelves_name like '%"+this.keyword+"%')"
			}
			
			if(this.scan_keyword!=''){
				this.where += " and (cInvCode = '"+this.scan_keyword+"' or shelves_code = '"+this.scan_keyword+"')"
			}
			
			this.where += " and warehouse_id = "+this.stock_id
			
			this.$Db.queryCount('kc',this.where).then(res => {
				this.total = res[0].num;//获得总页数
			})
			this.$Db.queryDataList('kc',this.currentPage,this.pageSize,this.where,'cInvCode','asc').then(res => {
				this.card_arr = res;
				uni.hideLoading();
			})
		}else{
			//20231204在线版
			this.pageinfo = {
				table:'kc',
				pagesize:this.pageSize,
				page:this.currentPage,
				order_str:'cBatch asc',
				condition_str:JSON.stringify({
				  keyword:this.keyword,
				  scan_keyword:this.scan_keyword,
				  warehouse_id:this.stock_id,
				  inv_id:this.inv_id
				})
			}
			
			this.$Api.getPageList(this.pageinfo).then(res=>{
				this.card_arr = res.data.data;
				this.total = res.data.total
				this.currentPage = res.data.current_page
				uni.hideLoading();
			}).catch(err=>{
				this.$Api.msg(err)
			})
			//
		}
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    },
	selectes(item) {
	  if (item == '') return
	  
	  let emit_name = ''
	  //存货展示页
	  if(this.flag == 'check'){
		  //监听盘点
		emit_name = 'get_kc'
	  }
	  
	  //扫描存货库区页面
	  if(this.flag == 'checksis'){
	  		emit_name = 'get_kc_sis'
	  }
	  
	  if(emit_name!=''){
		 //监听盘点
		 uni.navigateBack({
		 	delta: 1,
		 	success: () => {
		 	  uni.$emit(emit_name, item,this.index)
		 	}
		 }); 
	  }
	},
	reset(){
		this.scan_keyword = ''
		this.getlist()
	}
  },
  destroyed() {
  	this.globalData.toSecPage = ''
  },
  onLoad(e) {
	this.flag = e.flag
	this.$Func.setBarTitle('库存查询')
	this.globalData.toSecPage = 'kc'
	
	if(!isNaN(e.index)){
		this.index = e.index
	}
	
	if(e.stock_id!=undefined&&!isNaN(e.stock_id)){
		this.stock_id = e.stock_id
		if(e.inv_id!=undefined){
			this.inv_id = e.inv_id
		}
	}
	
	this.getlist()
	
	/* 监听存货扫码 */
	uni.$off("scan_kc")
	uni.$on("scan_kc", (res) => {
		//存货库存
		this.scan_keyword = res.info.number
		this.getlist()
	})
  }
}
</script>
<style lang="less">
</style>